﻿using System.Collections.Generic;
using System.Linq;
using System;
using Apps.Models.PUB;
using Apps.Models;
using Apps.Common;
using System.Linq.Expressions;

namespace Apps.BLL.PUB
{
    public partial class PUB_CAR_BLACKLISTBLL
    {

        public List<PUB_CARModel> GetCarNotInBlackList(ref GridPager pager, string queryStr)
        {
            try
            {
                var queryData = m_Rep.Context.PUB_CAR.AsQueryable();
                queryData = from m1 in queryData
                            where m1.LPR.Contains(queryStr) &&
                                !(from m2 in m_Rep.Context.PUB_CAR_BLACKLIST
                                  select m2.CAR_ID).Contains(m1.CAR_ID)
                            select m1;
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return TransHelper.TransExpList<PUB_CAR, PUB_CARModel>(queryData.ToList());
            }
            catch (Exception )
            {
                return null;
            }
        }
        public override List<PUB_CAR_BLACKLISTModel> GetList(ref GridPager pager, string queryStr)
        {
            var queryData = m_Rep.GetList(p => p.PUB_CAR.LPR.Contains(queryStr));
            var queryResult = from m1 in queryData
                              join m2 in m_Rep.Context.SysUser on m1.UPDATE_USER_ID equals m2.Id
                              select new PUB_CAR_BLACKLISTModel()
                              {
                                  CAR_ID = m1.CAR_ID,
                                  LPR = m1.PUB_CAR.LPR,
                                  REASON = m1.REASON,
                                  STATUS = m1.STATUS,
                                  UPDATE_TIME = m1.UPDATE_TIME,
                                  UPDATE_USER_ID = m1.UPDATE_USER_ID,
                                  UPDATE_USER_NAME = m2.TrueName
                              };
            pager.totalRows = queryResult.Count();
            //排序
            queryResult = LinqHelper.SortingAndPaging(queryResult, pager.sort, pager.order, pager.page, pager.rows);
            return queryResult.ToList();
        }

        public PUB_CAR_BLACKLISTModel GetLastBlackListById(string id)
        {
            var queryData = m_Rep.GetList(p => p.PUB_CAR.CAR_ID == id);
            var queryResult = from m1 in queryData
                              join m2 in m_Rep.Context.SysUser on m1.UPDATE_USER_ID equals m2.Id
                              select new PUB_CAR_BLACKLISTModel()
                              {
                                  CAR_ID = m1.CAR_ID,
                                  LPR = m1.PUB_CAR.LPR,
                                  REASON = m1.REASON,
                                  STATUS = m1.STATUS,
                                  UPDATE_TIME = m1.UPDATE_TIME,
                                  UPDATE_USER_ID = m1.UPDATE_USER_ID,
                                  UPDATE_USER_NAME = m2.TrueName
                              };
            return queryResult.FirstOrDefault();
        }
        
    }
}

